<template>
  <div class="setOA">
    <navBar title="设置OA账号" @onClickLeft="onClickLeft" />
    <div class="form">
      <van-form ref="form" @submit="onSubmit">
        <van-field
          class="van-field-isRequired"
          v-model="account"
          name="account"
          label="OA账号"
          placeholder="请输入OA账号"
          :rules="[{ required: true, message: '请填写OA账号' }]"
        />
        <van-field
          class="van-field-isRequired"
          v-model="password"
          name="password"
          type="password"
          label="OA密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写OA密码' }]"
        />
      </van-form>
      <div class="btn-box">
        <van-button round type="info" block @click="submit" class="mb15"
          >提交</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import navBar from "@cb/navBar.vue";
import { setOA } from "@a/user.js";
import { Toast } from "vant";
import { getMy } from "@u/index.js";
export default {
  components: {
    navBar
  },
  data() {
    return {
      account: "",
      password: ""
    };
  },
  created() {
    if (this.$store.state.user.ex_info) {
      this.account = this.$store.state.user.ex_info.oa_phone;
      this.password = this.$store.state.user.ex_info.oa_pwd;
    }
  },
  methods: {
    onClickLeft() {
      this.$router.back();
    },
    submit() {
      this.$refs["form"].submit();
    },
    onSubmit() {
      let params = {
        oa_phone: this.account,
        oa_pwd: this.password
      };

      if (
        this.$store.state.user.ex_info &&
        this.account == this.$store.state.user.ex_info.oa_phone &&
        this.password == this.$store.state.user.ex_info.oa_pwd
      ) {
        this.$message.error("您似乎没有什么改变");
        return;
      }

      setOA(params).then(res => {
        if (res.code == 200) {
          Toast.success("修改成功！");
          sessionStorage.setItem("isOATipOver", true);
          getMy(this);
          this.$router.replace('/user/home')
        }
      });
    }
  }
};
</script>

<style lang="less">
.setOA {
  height: 100%;
  width: 100%;

  .btn-box {
    width: 100%;
    box-sizing: border-box;
    padding: 15px 15px;
  }
  .form {
    margin-top: 80px;
    .van-form {
      .van-field {
        margin-bottom: 15px;
      }
    }
  }
}
</style>
